/*
    日志项目---扩展接口的测试样例
*/
#include <iostream>
#include <unistd.h>
#include "../logs/logging.h"


// 日志落地模块扩展
enum class timeGap
{
    SEC,
    MIN,
    HOUR,
    DAY
};

class rollSinkByTime : public logging::sink
{
public:
    rollSinkByTime(const std::string &basename, timeGap tg)
        : _basename(basename)
    {
        // 设置时间间隔
        switch (tg)
        {
        case timeGap::SEC:
            _time_gap = 1;
            break;
        case timeGap::MIN:
            _time_gap = 60;
            break;
        case timeGap::HOUR:
            _time_gap = 3600;
            break;
        case timeGap::DAY:
            _time_gap = 3600 * 24;
            break;
        }
        
        // 1.获取当前时间，计算当前所在时间段
        time_t t = logging::util::Date::getTime();
        _cur_time_seg = _time_gap == 1 ? t : t % _time_gap;

        // 2.创建一个具体的文件
        std::string filename = createNewFile();

        // 3.创建目录
        logging::util::File::createDirectory(logging::util::File::getFilePath(filename));

        // 4.打开文件
        _ofs.open(filename, std::ios::binary | std::ios::app);
        assert(_ofs.is_open());
    }

    void logSink(const char *data, size_t len)
    {
        // 1.判断是否超出时间段
        time_t t = logging::util::Date::getTime();
        int cur_seg = _time_gap == 1 ? t : t % _time_gap;
        if(cur_seg != _cur_time_seg) {
            _ofs.close();
            std::string newfile = createNewFile();
            _ofs.open(newfile, std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
            _cur_time_seg = cur_seg;
        }

        // 如果没有超过，则直接继续写入
        _ofs.write(data, len);
        assert(_ofs.good());
    }
private:
        std::string createNewFile()
        {
            // 通过基础文件名，创建出一个具体的带有时间的文件名
            // 1.获取当前时间
            time_t t = logging::util::Date::getTime();
            struct tm tmset;
            localtime_r(&t, &tmset);

            // 将基础文件名与时间后缀拼接构成一个完整的文件名
            std::stringstream ss;
            ss << _basename;
            ss << tmset.tm_year + 1900;
            ss << tmset.tm_mon + 1;
            ss << tmset.tm_mday;
            ss << tmset.tm_hour; 
            ss << tmset.tm_min;
            ss << tmset.tm_sec;
            ss << ".log";
            return ss.str();
        }
private:
    std::string _basename;
    std::ofstream _ofs;
    int _time_gap;
    int _cur_time_seg;
};


void test_Log()
{
    logging::logger::s_ptr p = logging::getLogger("sync-looper");
    int count = 5;
    while(count--) {
        UNIQUE_DEBUG("sync-looper", "测试同步日志器-%d", count);
        UNIQUE_INFO("sync-looper", "测试全局接口-%d", count);
        UNIQUE_WARNNING("sync-looper", "测试全局接口-%d", count);
        UNIQUE_ERROR("sync-looper", "测试全局接口-%d", count);
        UNIQUE_FATAL("sync-looper", "测试全局接口-%d", count);
        sleep(1);
    }
}
int main()
{
    logging::globalLoggerBuilder llb;
    llb.buildEnableUnsafe();
    llb.buildFormatter();
    llb.buildLimitRank(logging::logLevel::value::DEBUG);
    llb.buildLoggerName("sync-looper");
    llb.buildLoggerType(logging::loggerType::SYNC_LOGGER);
    llb.buildSinks<rollSinkByTime>("./file-log/sync-looper-", timeGap::SEC);
    logging::logger::s_ptr async_logger = llb.build();

    test_Log();

    return 0;
}
